﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Interface.Attendance;
using EzDesk.Utility.Extensions;
using EzDesk.Utility.Generic;
using EzDesk.Utility.TimerService;
using EzDesk.Utility.Json;


namespace EzDesk.Apps.ServiceImpl.Attendance
{
    public partial class AttendanceService : IAttendanceService
    {

        public void SendDailyAttendanceStatics()
        {
            // todo: 如果头一天没什么特别的事情就不用发Feed了

            DateTime attendanceDay = DateTime.Today.AddDays(-1);
            string dateStr = attendanceDay.ToFullDate();
            string dateEpoch = attendanceDay.ToEpoch().ToString();

            var allUserList = _ezApiClient.GetSimpleUserInfos("everyone");

            var dayAttendanceDetailList = allUserList
                .Select(user => GetDayAttendanceEvaluation(user.Id, attendanceDay).Result.FirstOrDefault());


            var absenseCount = dayAttendanceDetailList.Count(p => p.IsAbsense);
            var extraWorkCount = dayAttendanceDetailList.Count(p => p.IsExtraWork);

            var evaluationBuilder = new StringBuilder("  ");

            evaluationBuilder.AppendFormat("加班 {0} 人 ", extraWorkCount);
            evaluationBuilder.AppendFormat("旷工 {0} 人 ", absenseCount);

            var feedData = new Dictionary<string, string> {
                    {"dateStr", dateStr}, 
                    {"dateEpoch", dateEpoch},
                    {"evaluationStr", evaluationBuilder.ToString()}
                };

            // 发送目标：所有人事
            string targetMemberString = _ezApiClient
                .GetGroupByName("attendance_hr")
                .Memberstring;

            _ezApiClient.SendFeed(
                FeedTemplateStr_attendance_dailyAttendanceStatics,
                JsonHelper.ConvertToJson(feedData),
                targetMemberString
                );
        }


        public void SendDailyAttendanceReport()
        {
            // 目标用户，所有参与考勤的员工 (所有人)
            var memberstring = "everyone";

            var attendanceDay = DateTime.Today.AddDays(-1);

            var feedData = new Dictionary<string, string>
            {
                {"dateStr", attendanceDay.ToFullDate()},
                {"timeEpoch", attendanceDay.ToEpoch().ToString()},
                {"manHours", ""},
                {"workStatus", ""},
                {"evaluations", ""},
            };

            var feedId = _ezApiClient.SendFeed(
                FeedTemplateStr_attendance_dailyAttendanceReport,
                JsonHelper.ConvertToJson(feedData),
                memberstring, isAvailable: false
                );

            var targetUserList = _ezApiClient.GetSimpleUserInfos(memberstring);
            foreach (var user in targetUserList)
            {
                var evaluation = GetDayAttendanceEvaluation(user.Id, attendanceDay).Result.FirstOrDefault();
                if (!evaluation.IsNeedToWork && evaluation.IsExtraWork)
                {
                    // 如果不需要工作且没有加班则不发考勤统计
                    _ezApiClient.SetFeedIsAvailable(feedId, user.Id.ToString(), false);
                    continue;
                }

                var workStatusBuilder = new StringBuilder(" ");
                if (evaluation.IsExtraWork)
                {
                    workStatusBuilder.Append("(加班)");
                }
                if (evaluation.IsAbsense)
                {
                    workStatusBuilder.Append("(旷工)");
                }


                var evaluationsBuilder = new StringBuilder(" ");
                if (evaluation.IsLate)
                {
                    evaluationsBuilder.AppendFormat("迟到 {0} 分钟<ez:br />", evaluation.LateMiniutes);
                }
                if (evaluation.IsLeaveEarly)
                {
                    evaluationsBuilder.AppendFormat("早退 {0} 分钟<ez:br />", evaluation.EarlyMiniutes);
                }
                var feedDataEx = new Dictionary<string, string>
                {
                    {"manHours", evaluation.Manhours.DivEx(10)},
                    {"workStatus", workStatusBuilder.ToString()},
                    {"evaluations", evaluationsBuilder.ToString()},
                };
                _ezApiClient.UpdatePersonalFeedData(
                    feedId,
                    user.Id.ToString(),
                    JsonHelper.ConvertToJson(feedDataEx)
                    );
            }

            _ezApiClient.SetFeedIsAvailable(feedId, memberstring, true);
        }
    }
}
